<template>
  <div class="app-container">

    <el-form :inline="true" >

      <el-form-item>
        <!-- <el-input v-model="searchObj.name" placeholder="讲师"/> -->
        <el-autocomplete
          v-model="searchObj.name"
          :fetch-suggestions="querySearch"
          :trigger-on-focus="false"
          value-key="name"
          class="inline-input"
          placeholder="请输入讲师名称"
        />
      </el-form-item>

      <el-form-item>
        <el-select v-model="searchObj.level" clearable placeholder="职级">
          <el-option label="高级讲师" value="1"/>
          <el-option label="首席讲师" value="2"/>
        </el-select>
      </el-form-item>

      <el-form-item label="入驻时间">
        <el-date-picker
          v-model="searchObj.joinDateBegin"
          placeholder="开始时间"
          value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="-">
        <el-date-picker
          v-model="searchObj.joinDateEnd"
          placeholder="结束时间"
          value-format="yyyy-MM-dd" />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
      <el-button icon="el-icon-delete" @click="resetData()">清空</el-button>

    </el-form>

    <!-- 表格 -->
    <el-table :data="list" border stripe>
      <el-table-column type="index" width="50">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="avatar" label="头像">
        <template slot-scope="scope">
          <el-avatar :src="scope.row.avatar"/>
        </template>
      </el-table-column>

      <el-table-column prop="name" label="名称" width="80" />
      <el-table-column prop="level" label="头衔" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.level === 1" type="success" >高级讲师</el-tag>
          <el-tag v-if="scope.row.level === 2">首席讲师</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="intro" label="资历" />
      <el-table-column prop="sort" label="排序" width="60" />
      <el-table-column prop="joinDate" label="入驻时间" width="160" />

      <el-table-column label="操作">
        <template slot-scope="scope">
          <router-link :to="'/teacher/edit/' + scope.row.id">
            <el-button type="primary" size="small" icon="el-icon-edit">修改</el-button>
          </router-link>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="removeById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 100]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="currentChange"
      @size-change="sizeChange"
    />
  </div>
</template>

<script>
import teacherApi from '@/api/teacher'

export default{
  data() {
    return {
      list: [], // 讲师列表,
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 5, // 每页记录数
      circleUrl: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
      searchObj: {} // 查询表单
    }
  },

  created() {
    this.fetchData()
  },

  methods: {
    // 调用api模块，加载讲师列表
    fetchData() {
      teacherApi.pageList(this.page, this.limit, this.searchObj).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
      })
    },
    // 重置表单
    resetData() {
      this.searchObj = {}
      this.fetchData()
    },
    // 翻页
    currentChange(page) {
      this.page = page
      this.fetchData()
    },
    // 修改页面记录数
    sizeChange(size) {
      this.limit = size
      this.fetchData()
    },
    // 根据id删除讲师
    removeById(id) {
      // 询问是否删除
      this.$confirm('此操作将永久删除该讲师, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 执行删除操作
        teacherApi.removeById(id).then(response => {
          // 刷新页面
          this.fetchData()
          // 弹出成功提示
          this.$message({
            message: response.message,
            type: 'success'
          })
        })
      }).catch((err) => {
        if (err === 'cancel') {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        }
      })
    },

    // 输入建议
    querySearch(queryString, callback) {
      console.log('queryString', queryString)
      teacherApi.selectNameListByKey(queryString).then(response => {
        callback(response.data.nameList)
      })
    }
  }
}

</script>
